package org.jwcloud.project.module.zx.info.controller;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.jwcloud.project.module.zx.common.Resp;
import org.jwcloud.project.module.zx.db.domain.RtaItemSzCol;
import org.jwcloud.project.module.zx.db.domain.RtaItemSzInfo;
import org.jwcloud.project.module.zx.info.service.InfoService;
import org.jwcloud.project.module.zx.info.service.TodoListTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jw.mid.mm.comm.common.controller.CrudController;
import com.jw.mid.mm.comm.common.json.Response;
import com.jw.mid.mm.comm.common.service.CrudService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value = "API - Controlle", description = "项目数据上报", tags = "info")
@RestController
@RequestMapping("/rest/info")
public class InfoController extends CrudController {
	@Autowired
	private InfoService infoService;

	@Override
	public CrudService getService() {
		// TODO Auto-generated method stub
		return infoService;
	}

	@GetMapping("todoList")
	@ApiOperation(value = "代办任务列表", notes = "")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "prjid", value = "项目编号 ", required = true, dataType = "Integer", paramType = "query"), })
	public Resp<Response> todoList(HttpServletRequest request, @RequestParam Integer prjid) {

		Map<String, Object> userInfo = new HashMap<String, Object>();

		userInfo.put("USER_ID", request.getHeader("_session_user"));
		userInfo.put("ORG_ID", request.getHeader("_session_org"));

		TodoListTree todoListTree = infoService.todoList(prjid, userInfo);

		// TodoListTree todoListTree = new TodoListTree();
		Response<TodoListTree> response = new Response<TodoListTree>();
		response.setCode(0);
		response.setPayload(todoListTree);
//		return ResponseEntity.ok(response);
		return Resp.success(response);
	}

	@GetMapping("todoListForTask")
	@ApiOperation(value = "查询单任务代办任务列表", notes = "")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "prjid", value = "项目编号 ", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "taskid", value = "任务编号 ", required = true, dataType = "Integer", paramType = "query"), })
	public Resp<Response> todoListForTask(@RequestParam Integer prjid, @RequestParam Integer taskid) {

		TodoListTree todoListTree = infoService.todoListForTask(prjid, taskid);

		// TodoListTree todoListTree = new TodoListTree();
		Response<TodoListTree> response = new Response<TodoListTree>();
		response.setCode(0);
		response.setPayload(todoListTree);
//		return ResponseEntity.ok(response);
		return Resp.success(response);
	}

	@GetMapping("searchItemListAndInfo")
	@ApiOperation(value = "查询任务对应的要件列表及表头和数据", notes = "")
	@ApiImplicitParams({ @ApiImplicitParam(name = "prjId", value = "项目编号 ", required = true, paramType = "query"),
			@ApiImplicitParam(name = "workid", value = "工作编号 ", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "taskid", value = "任务编号 ", required = true, dataType = "Integer", paramType = "query") })
	public Resp<Response<?>> searchItemListAndInfo(@RequestParam Integer prjId, @RequestParam Integer workid,
			@RequestParam Integer taskid) {
		List<Map<String, Object>> rutList = new ArrayList<Map<String, Object>>();

		List<RtaItemSzInfo> rtaItemSzInfos = infoService.searchItemListByTaskId(taskid);
		for (RtaItemSzInfo rtaItemSzInfo : rtaItemSzInfos) {
			Map<String, Object> rut = new HashMap<String, Object>();
			rut.put("title", rtaItemSzInfo);
			Integer itemId = rtaItemSzInfo.getId();
			List<RtaItemSzCol> rtaItemSzCols = infoService.searchItemColList(itemId);
			rut.put("head", rtaItemSzCols);

			List<Map<String, Object>> list = infoService.searchItemInfo(prjId, workid, taskid, itemId);
			List<Map<String, Object>> bodyList = new ArrayList<>();
			for (Map map : list) {
				Map info = (Map) map.get("itemInfo");
				bodyList.add(info);
			}
			rut.put("body", bodyList);
			rutList.add(rut);
		}
		Response<List> response = new Response<List>();
		response.setCode(0);
		response.setPayload(rutList);
		// return ResponseEntity.ok(response);
		return Resp.success(response);
	}

	@GetMapping("searchItemList")
	@ApiOperation(value = "查询任务对应的要件列表", notes = "")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "taskid", value = "任务编号 ", required = true, dataType = "Integer", paramType = "query") })
	public Resp<Response<?>> searchItemList(@RequestParam Integer taskid) {

		List<RtaItemSzInfo> list = infoService.searchItemListByTaskId(taskid);
		Response<List> response = new Response<List>();
		response.setCode(0);
		response.setPayload(list);
		// return ResponseEntity.ok(response);
		return Resp.success(response);
	}

	@GetMapping("searchItemColList")
	@ApiOperation(value = "查询要件对应的项目列表", notes = "")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "itemId", value = "要件编号 ", required = true, dataType = "Integer", paramType = "query") })
	public Resp<Response<?>> searchItemColList(@RequestParam Integer itemId) {

		List<RtaItemSzCol> list = infoService.searchItemColList(itemId);
		Response<List<RtaItemSzCol>> response = new Response<List<RtaItemSzCol>>();
		response.setCode(0);
		response.setPayload(list);
		// return ResponseEntity.ok(response);
		return Resp.success(response);
	}

	@GetMapping("searchItemInfo")
	@ApiOperation(value = "查询要件信息", notes = "")
	@ApiImplicitParams({ @ApiImplicitParam(name = "prjId", value = "项目编号 ", required = true, paramType = "query"),
			@ApiImplicitParam(name = "taskId", value = "任务编号 ", required = true, paramType = "query"),
			@ApiImplicitParam(name = "workId", value = "工作编号 ", required = true, paramType = "query"),
			@ApiImplicitParam(name = "itemId", value = "要件编号 ", required = true, paramType = "query") })
	public Resp<Response<?>> searchItemInfo(@RequestParam Integer prjId, @RequestParam Integer workId,
			@RequestParam Integer taskId, @RequestParam Integer itemId) {

		List list = infoService.searchItemInfo(prjId, workId, taskId, itemId);
		Response<List> response = new Response<List>();
		response.setCode(0);
		response.setPayload(list);
		// return ResponseEntity.ok(response);
		return Resp.success(response);
	}

	@PostMapping("saveItemInfo")
	@ApiOperation(value = "保存要件信息", notes = "")
	@ApiImplicitParams({ @ApiImplicitParam(name = "prjId", value = "项目编号 ", required = true),
			@ApiImplicitParam(name = "workId", value = "工作编号 ", required = true),
			@ApiImplicitParam(name = "taskId", value = "任务编号 ", required = true),
			@ApiImplicitParam(name = "itemId", value = "要件编号 ", required = true),
			@ApiImplicitParam(name = "info", value = "上报数据 ", required = false) })
	public Resp<Response<?>> saveItemInfo(HttpServletRequest request, @RequestBody Map<String, Object> info) {

		Map userInfo = new HashMap();

		userInfo.put("USER_ID", request.getHeader("_session_user"));
		userInfo.put("ORG_ID", request.getHeader("_session_org"));
		//
		Integer prjId = (Integer) info.get("prjId");
		Integer workId = (Integer) info.get("workId");
		Integer taskId = (Integer) info.get("taskId");
		Integer itemId = (Integer) info.get("itemId");
		infoService.saveItemInfo(prjId, workId, taskId, itemId, info, userInfo);
		Response<?> response = new Response<List>();
		response.setCode(0);
		// response.setPayload(list);
		// return ResponseEntity.ok(response);
		return Resp.success(response);
	}

	@PostMapping("updateItemInfo")
	@ApiOperation(value = "修改要件信息", notes = "")
	@ApiImplicitParams({ @ApiImplicitParam(name = "prjId", value = "项目编号 ", required = true, paramType = "query"),
			@ApiImplicitParam(name = "infoId", value = "信息编号 ", required = true, paramType = "query"),
			@ApiImplicitParam(name = "info", value = "上报数据 ", required = true, paramType = "query") })
	public Resp<Response<?>> updateItemInfo(@RequestBody Map<String, Object> info) {
		Integer prjId = (Integer) info.get("prjId");
		String infoId = (String) info.get("infoId");
		infoService.updateItemInfo(prjId, infoId, info);
		Response<List> response = new Response<List>();
		response.setCode(0);
		// response.setPayload(list);
		// return ResponseEntity.ok(response);
		return Resp.success(response);
	}

	@PostMapping("repleaseItemInfo")
	@ApiOperation(value = "修改要件代替说明", notes = "")
	@ApiImplicitParams({ @ApiImplicitParam(name = "prjId", value = "项目编号 ", required = true, paramType = "query"),
			@ApiImplicitParam(name = "infoId", value = "信息编号 ", required = true, paramType = "query"),
			@ApiImplicitParam(name = "msg", value = "代替说明 ", required = true, paramType = "query") })
	public Resp<Response<?>> repleaseItemInfo(@RequestParam Integer prjId, @RequestParam String infoId,
			@RequestParam String msg) {

		infoService.repleaseItemInfo(prjId, infoId, msg);
		Response<List> response = new Response<List>();
		response.setCode(0);
		// response.setPayload(list);
		// return ResponseEntity.ok(response);
		return Resp.success(response);
	}

	@DeleteMapping("delItemInfo")
	@ApiOperation(value = "删除要件信息", notes = "")
	@ApiImplicitParams({ @ApiImplicitParam(name = "prjId", value = "项目编号 ", required = false, paramType = "query"),
			@ApiImplicitParam(name = "infoId", value = "信息编号 ", required = true, paramType = "query") })
	public Resp<Response<?>> delItemInfo(@RequestParam Integer prjId, @RequestParam String infoId) {

		infoService.delItemInfo(prjId, infoId);
		Response<List> response = new Response<List>();
		response.setCode(0);
		// response.setPayload(list);
		// return ResponseEntity.ok(response);
		return Resp.success(response);
	}

	@DeleteMapping("delItemInfos")
	@ApiOperation(value = "批量删除要件信息", notes = "")
	@ApiImplicitParams({ @ApiImplicitParam(name = "prjId", value = "项目编号 ", required = false, paramType = "query"),
			@ApiImplicitParam(name = "infoId", value = "信息编号 ", required = true, paramType = "query") })
	public Resp<Response<?>> delItemInfos(@RequestParam Integer prjId, @RequestParam String infoIds) {

		String[] ids = infoIds.split(":");
		for (String infoId : ids) {
			infoService.delItemInfo(prjId, infoId);
		}
		Response<List> response = new Response<List>();
		response.setCode(0);
		// response.setPayload(list);
		// return ResponseEntity.ok(response);
		return Resp.success(response);
	}

//	@PostMapping("finishTask")
//	@ApiOperation(value = "完成任务", notes = "")
//	@ApiImplicitParams({ @ApiImplicitParam(name = "prjId", value = "项目编号 ", required = true),
//			@ApiImplicitParam(name = "taskId", value = "任务编号 ", required = true), })
//	public Resp<Response<?>> finishTask(HttpServletRequest request,@RequestParam Integer prjId,@RequestParam Integer taskId ) {
//
////		Integer prjId = (Integer) info.get("prjId");
////		Integer taskId = (Integer) info.get("taskId");
//		Map userInfo = new HashMap();
//
//		userInfo.put("USER_ID", request.getHeader("_session_user"));
//		userInfo.put("ORG_ID", request.getHeader("_session_org"));
//		infoService.finishTask(prjId, taskId, userInfo);
//		Response<?> response = new Response<List>();
//		response.setCode(0);
//		// response.setPayload(list);
//		// return ResponseEntity.ok(response);
//		return Resp.success(response);
//	}

	@PostMapping("commitTask")
	@ApiOperation(value = "提交任务审批", notes = "")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "prjId", value = "项目编号 ", dataType = "Integer", paramType = "query", required = true),
			@ApiImplicitParam(name = "workId", value = "工作编号 ", dataType = "Integer", paramType = "query", required = true),
			@ApiImplicitParam(name = "taskId", value = "任务编号 ", dataType = "Integer", paramType = "query", required = true) })
	public Resp<Response<?>> commitTask(HttpServletRequest request, @RequestParam Integer prjId,
			@RequestParam Integer workId, @RequestParam Integer taskId) {

//		Integer prjId = (Integer) info.get("prjId");
//		Integer taskId = (Integer) info.get("taskId");
		Map userInfo = new HashMap();

		userInfo.put("USER_ID", request.getHeader("_session_user"));
		userInfo.put("ORG_ID", request.getHeader("_session_org"));

		infoService.commitTask(prjId, workId, taskId, userInfo);
		Response<?> response = new Response<List>();
		response.setCode(0);
		// response.setPayload(list);
		// return ResponseEntity.ok(response);
		return Resp.success(response);
	}
}
